package com.example.springbootdemo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String username, String password, HttpSession session) {
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        System.out.println(username + " " + password);
        if("admin".equals(username) && "admin".equals(password)) {
            session.setAttribute("username", "admin");
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = null;
        if(session != null) {
            username = (String) session.getAttribute("username");
        }
        return username;
    }
}
